//
//  PersonInfoViewController.m
//  QcMedical1
//
//  Created by ios001 on 2016/12/21.
//  Copyright © 2016年 jiawen Du. All rights reserved.
//

#import "PersonInfoViewController.h"
#import "PersonDataView.h"
#import "Users.h"
#import "PersonDataModifyView.h"

@interface PersonInfoViewController ()<UITableViewDelegate,UITableViewDataSource,PersonDataModifyViewDelegate>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) UIView *coverView;

@property (nonatomic, strong) Users *currentUser;

@property (nonatomic ,strong) NSArray <NSArray *>*dataArray;

@property (nonatomic, weak) PersonDataModifyView *modifyView;

@end

@implementation PersonInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}


- (Users *)currentUser{
    if (!_currentUser) {
        //解档
        self.currentUser = [Users unarchiver];
    }
    return _currentUser;
}

- (NSArray<NSArray *> *)dataArray{
    if (!_dataArray) {
        NSMutableArray *mutable = [NSMutableArray array];
        
        //身高数组
        NSMutableArray *heightArray = [NSMutableArray array];
        for (int i = 200; i <=500; i++) {
            [heightArray addObject:@(i*1.0 / 2)];
        }
        [mutable addObject:heightArray];
        
        //体重数组
        NSMutableArray *weightArray = [NSMutableArray array];
        for (int i = 100; i <= 300; i++) {
            [weightArray addObject:@(i*1.0 / 2)];
        }
        [mutable addObject:weightArray];
        
        //读取plist省份数组
        NSMutableArray *provinceArr = [NSMutableArray array];
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"province.plist" ofType:nil];
        NSArray <NSDictionary *>*dictArr = [NSPropertyListSerialization propertyListWithData:[NSData dataWithContentsOfFile:filePath] options:0 format:0 error:NULL];
        [dictArr enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [provinceArr addObject:obj[@"name"]];
        }];
        [mutable addObject:provinceArr];
        
        _dataArray = mutable.copy;
    }
    return _dataArray;
}


- (void)setupUI{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    self.tableView = tableView;
    [self.view addSubview:tableView];
    
    //个人信息
    PersonDataView *personDataView = [PersonDataView personDataView];
    personDataView.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 120);
    tableView.tableHeaderView = personDataView;
    tableView.tableFooterView = [UIView new];
}

#pragma mark - dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CellID"];
    NSString *optionName, *optionNum;
    switch (indexPath.row) {
        case 0:
            optionName = @"身高";
            optionNum = [NSString stringWithFormat:@"%.1f cm",self.currentUser.height];
            break;
        case 1:
            optionName = @"体重";
            optionNum = [NSString stringWithFormat:@"%.1f kg",self.currentUser.weight];
            break;
        case 2:
            optionName = @"省份";
            optionNum = self.currentUser.address;
            break;
            
        default:
            break;
    }
    cell.textLabel.text = optionName;
    cell.detailTextLabel.text = optionNum;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}


#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //遮罩
    UIView *coverView = [[UIView alloc] initWithFrame:self.view.bounds];
    coverView.backgroundColor = [UIColor colorWithWhite:0.7 alpha:0.3];
    _coverView = coverView;
    
    //PickerView
    PersonDataModifyView *modifyView = [PersonDataModifyView personDataModifyView];
    modifyView.dataArray = self.dataArray[indexPath.row];
    _modifyView = modifyView;
    modifyView.delegate = self;
    [tableView insertSubview:coverView belowSubview:modifyView];
    
    modifyView.bounds = CGRectMake(0, 0, 280, 350);
    modifyView.center = CGPointMake(self.tableView.center.x, self.tableView.center.y-50);
    modifyView.layer.cornerRadius = 10;
    modifyView.layer.masksToBounds = YES;
    
    [tableView addSubview:modifyView];
    modifyView.transform = CGAffineTransformMakeScale(0.01, 0.01);
    
    [self show];
    
}

- (void)show{
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:0 animations:^{
        
        self.modifyView.transform = CGAffineTransformMakeScale(1, 1);
        
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark - modifyView代理
- (void)modifyViewDidmiss{
    [self.coverView removeFromSuperview];
}



- (void)modifyViewCommitWithString:(NSString *)string{
    
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    switch (indexPath.row) {
        case 0:
            self.currentUser.height = [string floatValue];
            string = [string stringByAppendingString:@" cm"];
            break;
        case 1:
            self.currentUser.weight = [string floatValue];
            string = [string stringByAppendingString:@" kg"];
            break;
        case 2:
            self.currentUser.address = string;
            break;
            
        default:
            break;
    }
    cell.detailTextLabel.text = string;
    
    [Users archiver:self.currentUser];
}

//- (void)setupUI{
//    
//    UIImageView *imageView = [[UIImageView alloc] init];
//    imageView.image = [UIImage imageNamed:@"bbx6"];
//    [self.view addSubview:imageView];
//    
//    UILabel *lb_name = [[UILabel alloc] init];
//    lb_name.text = @"姓名: 王二才";
//    lb_name.backgroundColor = [UIColor lightGrayColor];
//    [self.view addSubview:lb_name];
//    UILabel *lb_age = [[UILabel alloc] init];
//    lb_age.text = @"年龄: 25岁";
//    lb_age.backgroundColor = [UIColor lightGrayColor];
//    [self.view addSubview:lb_age];
//    
//    UILabel *lb_prefer = [[UILabel alloc] init];
//    lb_prefer.text = @"兴趣爱好: 把妹";
//    lb_prefer.backgroundColor = [UIColor lightGrayColor];
//    [self.view addSubview:lb_prefer];
//    
//    //约束
//    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self.view);
//        make.top.equalTo(self.view).mas_offset(50+64);
//        make.width.height.mas_equalTo(80);
//    }];
//    [lb_name mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(imageView.mas_bottom).mas_offset(50);
//        make.left.equalTo(self.view).mas_offset(30);
//        make.right.equalTo(self.view).mas_offset(-30);
//    }];
//    [lb_age mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(lb_name.mas_bottom).mas_offset(30);
//        make.left.equalTo(self.view).mas_offset(30);
//        make.right.equalTo(self.view).mas_offset(-30);
//    }];
//    [lb_prefer mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(lb_age.mas_bottom).mas_offset(50);
//        make.left.equalTo(self.view).mas_offset(30);
//        make.right.equalTo(self.view).mas_offset(-30);
//    }];
//}

@end
